package gnv;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import com.swtdesigner.SWTResourceManager;

public class Calendario {

	protected Shell shell;

	public String fecha;
	
	Calendario() {
		super();
	}

	
	
	public String getFecha() {
		return fecha;
	}



	public void setFecha(String fecha) {
		this.fecha = fecha;
	}



	/**
	 * Launch the application
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Calendario window = new Calendario();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window
	 */
	public void open() {
		final Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	/**
	 * Create contents of the window
	 */
	protected void createContents() {
		
		
		shell = new Shell(SWT.TITLE | SWT.SYSTEM_MODAL | SWT.BORDER | SWT.CLOSE);
		shell.setImage(SWTResourceManager.getImage(Calendario.class, "genoa.png"));
		shell.setLayout(new GridLayout());
		shell.setSize(200, 220);		
		
		
		int mWidth = 200;
		int mHeight = 220;
     	Rectangle pRec = Display.getCurrent().getClientArea();
     	int nLeft = (pRec.width - mWidth) / 2;
     	int nTop = (pRec.height - mHeight) / 2;
		shell.setBounds(nLeft, nTop, mWidth, mHeight);
		
		shell.setText("Calendario");

		final Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayout(new GridLayout());

		final DateTime dateTime = new DateTime(composite, SWT.CALENDAR | SWT.SHADOW_OUT);
		dateTime.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));

		final Button seleccionarButton = new Button(composite, SWT.NONE);
		seleccionarButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				
				String dia, mes = null;
				
				if (dateTime.getDay() <= 9){
					dia = "0"+dateTime.getDay();
				}else {
					dia = ""+dateTime.getDay();
				}
				
				if (dateTime.getMonth() <= 9){
					mes = "0"+(dateTime.getMonth()+1);
				}else {
					mes = ""+(dateTime.getMonth()+1);
				}

				setFecha(dia+"-"+mes+"-"+dateTime.getYear());
				shell.close();
			}
		});
		seleccionarButton.setSelection(true);
		final GridData gd_seleccionarButton = new GridData(SWT.CENTER, SWT.CENTER, false, false);
		gd_seleccionarButton.heightHint = 30;
		seleccionarButton.setLayoutData(gd_seleccionarButton);
		seleccionarButton.setText("Seleccionar");
		
		shell.pack();
		//
	}

}
